"设置终端中的truecolor
set termguicolors
"打开行号nu和相对行号rnu
set nu
set lsp=5
"状态栏标尺，显示光标的行号和列号
set ruler
"设定窗口大小
"set lines=34 columns=72
"""""""""搜索""""""""
"搜索时高亮
set hls
"搜索时忽略大小写
set ignorecase
"打开实时搜索
set incsearch
"""""""""""""""""""""
"高亮当前行
set cursorline
"打开语法高亮
syntax on
"设置缩进，空格与制表符
set tabstop=4	 "设置tab宽4个空格
set expandtab	 "设置空格代替tab, set noexpandtab表示用制表符表示缩进
set softtabstop=4 "在编辑模式下按退格键时退回的长度
set shiftwidth=4  "表示每一级的缩进长度，一般与softtabstop保持一致
"unnamedplus 对应+寄存器；即ctrl + c,ctrl + v桌面系统剪贴板，unnamed 对应*寄存器，即x桌面剪贴板（用鼠标选择复制，用鼠标中键粘贴）
set clipboard^=unnamed,unnamedplus
"支持鼠标
set mouse=a
"命令行高度
set cmdheight=2
"显示输入的命令
set showcmd
"""""""""""""""""GUI option"""""""""""""""""
set guioptions=mtrb
"""""""""""""""""""状态栏设置"""""""""""""""""""""""""""""""""""""""""""""
" 设置状态栏
set laststatus=2
"""""""""""""状态行根据状态的不同，显示不同的颜色"""""""""""""""""""
function! InsertStatuslineColor(mode)
if a:mode == 'i'
	hi statusline guibg=#FF0000
elseif a:mode == 'r'
	hi statusline guibg=#0000FF
else
endif
endfunction
au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertLeave * hi statusline guibg=#FFA500 guifg=#FFFFFF
hi statusline guibg=#BEBEBE guifg=#000000
""""""""""""""""""""""""""""参考 https://zhuanlan.zhihu.com/p/25494323
"设置状态栏信息及颜色
set statusline=
set statusline +=%4*%n%m%r%h%w%*
set statusline +=%F
set statusline +=%=%1*[%2*%{&ff}:%{&fenc!=''?&fenc:&enc}%1*]
set statusline +=[%2*%Y%1*]
set statusline +=[%2*%03v:%03l%1*/%3*%L(%p%%)%1*]
set statusline +=%4*%w%h%r%m%n%*
hi User1 guifg=#BEBEBE
hi User2 guifg=#11DB00
hi User3 guifg=#FFFFFF
hi User4 guifg=#FF0000
hi User5 guifg=#a0ee40
"""""""""""""""""""ywvim设置"""""""""""""""""""""""""""""""""""""""""""""
let g:ywvim_ims=[ 
            \['wb98', '五笔98', 'wubi98.ywvim'], 
            \['py', '拼音', 'pinyin.ywvim'], 
            \]

let g:ywvim_py = { 'helpim':'wb98', 'gb':0 }
let g:ywvim_zhpunc = 1
let g:ywvim_listmax = 5
let g:ywvim_esc_autoff = 0
let g:ywvim_autoinput = 0
let g:ywvim_intelligent_punc=1
let g:ywvim_circlecandidates = 1
let g:ywvim_helpim_on = 0
let g:ywvim_matchexact = 0
let g:ywvim_chinesecode = 1
let g:ywvim_gb = 0
let g:ywvim_preconv = 'g2b'
let g:ywvim_conv = ''
let g:ywvim_lockb = 0
"""""""""""""""""""""""文件类型"""""""""""""""""""""""""""""""
" 侦测文件类型，不可以设置为 on，否则下面生成文件头的命令不生效。 
filetype detect 
" 载入文件类型插件 
filetype plugin on 
" 为特定文件类型载入相关缩进文件 
filetype indent on 
""""""""""""""""""""""为shell/python文件创建专有设置"""""""""""""""""""""""
autocmd FileType python set tabstop=4 shiftwidth=4 expandtab ai
autocmd FileType java set tabstop=4 shiftwidth=4 expandtab ai
autocmd FileType c,cpp set tabstop=4 shiftwidth=4 expandtab ci
"根据文件类型自动插入文件头
""""""" lua 导入设置，将lua专属配置放在一个独立的lua.vimrc文件里面
source ~/.vim/rc/lua.vimrc
""""""" 并在templates 目录下有 luaTmp.txt文件。
autocmd BufNewFile *.html :0r ~/.vim/templates/htmlTmp.txt
" auto add file header
autocmd BufNewFile *.py :0r ~/.vim/templates/pythonTmp.txt
autocmd BufNewFile *.py ks|call FileName()|'s
autocmd BufNewFile *.py ks|call Version()|'s
autocmd BufNewFile *.py ks|call CreatedTime()|'s
autocmd BufNewFile *.py ks|call Author()|'s
autocmd BufNewFile *.py ks|call Licence()|'s
autocmd BufNewFile *.py ks|call Desc()|'s
autocmd BufNewFile *.py normal 10G$v18h

autocmd BufNewFile *.sh :0r ~/.vim/templates/shellTmp.txt
autocmd BufNewFile *.sh ks|call FileName()|'s
autocmd BufNewFile *.sh ks|call Version()|'s
autocmd BufNewFile *.sh ks|call CreatedTime()|'s
autocmd BufNewFile *.sh ks|call Author()|'s
autocmd BufNewFile *.sh ks|call Licence()|'s
autocmd BufNewFile *.sh ks|call Desc()|'s
autocmd BufNewFile *.sh normal 9G$v18h

autocmd BufNewFile *.c :0r ~/.vim/templates/cTmp.txt
autocmd BufNewFile *.c ks|call FileName()|'s
autocmd BufNewFile *.c ks|call Version()|'s
autocmd BufNewFile *.c ks|call CreatedTime()|'s
autocmd BufNewFile *.c ks|call Author()|'s
autocmd BufNewFile *.c ks|call Licence()|'s
autocmd BufNewFile *.c ks|call Desc()|'s
autocmd BufNewFile *.c normal 7G$v18h

autocmd BufNewFile *.java :0r ~/.vim/templates/javaTmp.txt
autocmd BufNewFile *.java ks|call FileName()|'s
autocmd BufNewFile *.java ks|call Version()|'s
autocmd BufNewFile *.java ks|call CreatedTime()|'s
autocmd BufNewFile *.java ks|call Author()|'s
autocmd BufNewFile *.java ks|call Licence()|'s
autocmd BufNewFile *.java ks|call Desc()|'s
autocmd BufNewFile *.java normal 7G$v18h

fun! FileName()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/File.*/s/File.*/File 		: " .expand("%")
endfun

fun! Version()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/Version.*/s/Version.*/Version		: 0.1" 
endfun

fun! CreatedTime()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/Date.*/s/Date.*/Date		: " .strftime("%Y-%m-%d %H:%M")
endfun

fun! Author()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/Author.*/s/Author.*/Author		: Knat" 
endfun

fun! Licence()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/Licence.*/s/Licence.*/Licence		:" "Copyright © " .strftime("%Y"). " Knat All Rights Reserved."
endfun

fun! Desc()
	if line("$") > 10
		let l = 10
	else
		let l = line("$")
	endif
	exe "1," . l . "g/Desc.*/s/Desc.*/Desc		: " .strftime("%Y-%m-%d %T")
endfun
" end auto add file header
"""""""""""""将log/md5 视为 text 类型"""""""""""""""""""
au BufRead,BufNewFile *.log 	setfiletype text
au BufRead,BufNewFile *.md5 	setfiletype text
au BufRead,BufNewFile *.md,*.mkd,*.markdown	setfiletype markdown
"au BufRead,BufNewFile *.md	setfiletype text
"au BufRead,BufNewFile *.mkd	setfiletype text
""""""""""""根据终端或GUI界面来选择不同的colorscheme""""""""""""""""
if has("gui_running")
    colorscheme wombat256
else
    colorscheme onedark
    let ayucolor="mirage"
	"根据文件类型，选择不同高亮
	autocmd ColorScheme janah highlight Normal ctermbg=235
	au BufRead,BufNewFile *.txt 	colorscheme janah
	au BufRead,BufNewFile *.c	colorscheme moria
endif
""""""""""""""""""""""快捷键映射hotkeymap""""""""""""""""""""""""""""""""""
"键盘映射模式
"
"    map : 正常模式，可视化模式和运算符模式可用
"    nmap ：正常模式可用
"    vmap ：可视化模式可用
"    omap ：运算符模式可用
"    map! ：插入模式和命令行模式可用
"    imap ：插入模式可用
"    cmap ：命令行模式可用
vmap <C-C> "+y
nmap <C-P> "+gP
map  tn  <Esc>:tabnew<CR>
"插入模式时 jk进入normal模式
inoremap jk <Esc>
inoremap kj <Esc>
nnoremap  txt  <Esc>:set filetype=txt<CR>
nnoremap  md  <Esc>:set filetype=markdown<CR>zR
au BufEnter * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au BufLeave * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'
nnoremap  ww  <Esc>:w<CR>
nnoremap  cc  <Esc>ggVG"+y
"默认<Leader>为"\"反斜杠
nnoremap  <Leader>ww  <Esc>:source %<CR>
map! <S-Insert> <C-R>+
"""""""""""""""""""""光标居中"""""""""""""""""""""""""""""
"单ESC会有进入替换模式的问题
nnoremap <esc><esc> :nohl<cr>
noremap j gjzz
noremap k gkzz
noremap n nzz
noremap <s-n> <s-n>zz
"noremap * *zz
"noremap # #zz
"noremap <c-o> <c-o>zz
"noremap <c-i> <c-i>zz
"""""""""""""""""""""C语言编译执行快捷键"""""""""""""""""""""""""""""
"用gcc快速编译当前文件
"nnoremap <Leader>g <Esc>:!gcc % -o %:r.exe<CR>
nnoremap <Leader>g <Esc>:AsyncRun -mode=term -pos=xterm gcc % -o %:r.exe<CR>
"快速执行当前目录下编译出来的文件
"nnoremap <Leader>r <Esc>:!./%:r.exe<CR>
nnoremap <Leader>r <Esc>:AsyncRun -mode=term -pos=xterm ./%:r.exe<CR>
""""""""""""""""打开文件时依次尝试编码"""""""""""""""""""""
"解决"modifiable" is off的问题
set modifiable
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
if has("win32")
	set fileencoding=chinese
else
	set fileencoding=utf-8
endif
"设置新文件的编辑为utf-8
set encoding=utf-8
"解决中文标点如①②→——等显示不全的问题
set ambiwidth=double
"设置字体
set guifont=DejaVu\ Sans\ Mono:h14
set guifontwide=DejaVu\ Sans\ Mono:h14
"彩虹括号增强版 (Rainbow Parentheses Improved)
"https://github.com/luochen1990/rainbow
let g:rainbow_active = 1 "set to 0 if you want to enable it later via :RainbowToggle
	let g:rainbow_conf = {
	\	'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick'],
	\	'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
	\	'operators': '_,_',
	\	'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold', 'start=/</ end=/>/ fold'],
	\	'separately': {
	\		'*': {},
	\		'tex': {
	\			'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/'],
	\		},
	\		'lisp': {
	\			'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
	\		},
	\		'vim': {
	\			'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/', 'start=/{/ end=/}/ fold', 'start=/(/ end=/)/ containedin=vimFuncBody', 'start=/\[/ end=/\]/ containedin=vimFuncBody', 'start=/{/ end=/}/ fold containedin=vimFuncBody'],
	\		},
	\		'html': {
	\			'parentheses': ['start=/\v\<((area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)[ >])@!\z([-_:a-zA-Z0-9]+)(\s+[-_:a-zA-Z0-9]+(\=("[^"]*"|'."'".'[^'."'".']*'."'".'|[^ '."'".'"><=`]*))?)*\>/ end=#</\z1># fold'],
	\		},
	\		'css': 0,
	\	}
	\}
""""""""""""""""""VIM-Plug""""""""""""""""
call plug#begin()
	Plug 'mhinz/vim-startify'                       " vim 启动页
	Plug 'lilydjwg/fcitx.vim', {'branch': 'fcitx5'} " fcitx自动中英文切换
	Plug 'jiangmiao/auto-pairs'                     " 成对符号自动补全
	Plug 'godlygeek/tabular'                        " 表格格式化工具
	Plug 'plasticboy/vim-markdown'                  " markdown 工具
	Plug 'hotoo/pangu.vim'                          " 中文文本排版优化
	Plug 'vim-voom/VOoM'                            " 预览并在侧栏提供目录
	Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
	Plug 'luochen1990/rainbow' 			" 彩虹括号
	Plug 'mbbill/fencview'     			" 格式检测
	Plug 'yegappan/mru'        			" 最近打开文件
	Plug 'vim-scripts/ywvim'   			" 内置输入法
	Plug 'machakann/vim-sandwich'			"选中文本添加成对符号
	Plug 'Yggdroot/indentLine'
	Plug 'wsdjeg/ChineseLinter.vim', {'for': 'text'}	"中文文档语言规范检查
	Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
	Plug 'junegunn/fzf.vim'
	Plug 'skywind3000/vim-auto-popmenu'				"轻量级补全插件https://github.com/skywind3000/vim-auto-popmenu/
	Plug 'skywind3000/vim-dict'					"补全插件的字典服务
	Plug 'SirVer/ultisnips'					"自定义补全文件
	Plug 'honza/vim-snippets'				"自定义补全
	Plug 'preservim/nerdcommenter'				"注释插件
	Plug 'skywind3000/asynctasks.vim'			"异步运行命令
	Plug 'skywind3000/asyncrun.vim'				"配套的任务管理工具
call plug#end()
"""""""""""""""""""""""vim-auto-popmenu补全插件"""""""""""""""""""""""
" 设定需要生效的文件类型，如果是 "*" 的话，代表所有类型
let g:apc_enable_ft = {'c':1, 'python':1, 'markdown':1, 'php':1}
" " 设定从字典文件以及当前打开的文件里收集补全单词，详情看 ':help cpt'
set cpt=.,k,w,b
" " 不要自动选中第一个选项。
set completeopt=menu,menuone,noselect
" " 禁止在下方显示一些啰嗦的提示
set shortmess+=c
"常用命令
"ApcEnable：为当前文档开启补全（比如你没有设置上面的 g:apc_enable_ft 时）
"ApcDisable：为当前文档禁用补全 
"""""""""""""""""""""""""""Asyncrun & Asynctasks""""""""""""""""""""""""
let g:asyncrun_open = 6	 	"自动打开quickfix 窗口
"配置目录 ~/.config/nvim/tasks.ini
"命令:AsyncTaskList查看所有tasks.ini
"设定快捷键
noremap <silent><f5> :AsyncTask file-run<cr>
noremap <silent><f9> :AsyncTask file-build<cr>
"在标签页打开终端显示输出结果，配合tasks.ini中的output=terminal使用
"let g:asynctasks_term_pos = 'tab'
let g:asynctasks_term_pos = 'external'
"""""""""""""""ultisnips配置"""""""""""""""""
"自定义目录 mysnippets
let g:UltiSnipsSnippetDirectories=["UltiSnips","mysnippets"]
"自定义版本文件 all.snippets
""""""""""""""Startify""""""""""""""
let g:startify_bookmarks = [ {'c': '~/.vimrc'}, { 'n': '~/.config/nvim/init.vim' }, {'s': '~/.config/nvim/mysnippets/all.snippets'} ]
""""""""""vim-Markdown""""""""""
let g:vim_markdown_folding_disabled = 1
let g:vim_markdown_folding_style_pythonic = 1
let g:vim_markdown_toc_autofit = 1
""""""""""""Startify Custom""""""""""""""""
let g:startify_custom_header = [				      
				\'	::    ::  :::::::    ::::    ::    ::  ::::  ::     :: ',
				\'	:::   ::  ::       ::    ::  ::    ::   ::   :: : : :: ',
				\'	:: :  ::  ::::::   ::    ::  ::    ::   ::   ::  :  :: ',
				\'	::  : ::  ::       ::    ::   ::  ::    ::   ::     :: ',
				\'	::    ::  :::::::    ::::       ::     ::::  ::     :: ',
				\]                                                         
let g:startify_custom_footer = [
            \ '		+------------------------------+',
            \ '		|      1.01 ^ 365 =  37.78     |',
            \ '		|      0.99 ^ 365 =   0.02     |',
            \ '		|              `/ /\ () `/ |_| |',
            \ '		+----------------+-------------+',
            \]
